package com;


import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class ListAndLambda {
    public static void main(String[] args) {
        User user=new User("张三",20,"男","中国");
        User user1=new User("tom",19,"男","漂亮国");
        User user2=new User("乐迪",10,"男","英国");
        User user3=new User("张林凯",10,"人妖","无");
        User user4=new User("王昭君",40,"女","中国");
       List<User> list=new ArrayList<User>();
       list.add(user);
       list.add(user1);
       list.add(user2);
       list.add(user3);
       list.add(user4);
       list.stream().filter(u->u.getAge()<20).forEach(u->{
           System.out.println(u.toString());
       });
       //添加
        list.stream().forEach(a->{
            a.setBigDecimal(new BigDecimal(9));
        });
       //年龄和
       int numAge=list.stream().mapToInt(User::getAge).sum();
        BigDecimal sumAge=list.stream().map(User::getBigDecimal).reduce(BigDecimal.ZERO,BigDecimal::add);
        System.out.println("总年龄"+numAge+":"+sumAge);
       //获取name
       List<String> stringList=list.stream().map(l->l.getName()).collect(Collectors.toList());
       stringList.forEach(l->{
           System.out.println(l);
       });

       String names=list.stream().map(User::getName).collect(Collectors.joining(","));
        System.out.println(names);
       //获取最大最小年龄
        int maxAge=list.stream().map(User::getAge).max(Integer::compareTo).get();
        int minAge=list.stream().map(l->l.getAge()).min(Integer::compareTo).get();
        System.out.println("最大年龄是："+maxAge+"最小年龄："+minAge);
        //分组
        Map<String,List<User>> map=list.stream().collect(Collectors.groupingBy(User::getSex));
        for (String u: map.keySet()) {
            System.out.println(u+""+map.get(u));
            List<User> userList=map.get(u);
            System.out.println(u+":"+userList);
        }
        //排序
        list.sort(Comparator.comparing(User::getAge));
        System.out.println(list);
    }




}
